/**
 * 
 */
package com.asdf.tests;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.asdf.external.DBUsageDetails;

/**
 * @author ilia
 *
 */
public class SimpleStat
{
	List<SimpleUsage> usage = new ArrayList<SimpleUsage>();

	SimpleStat() {}

	SimpleStat(SimpleUsage u) {
		add(u);
	}
	SimpleStat(String airportId, String userId, Date date, int duration) {
		add(airportId, userId, date, duration);
	}
	SimpleStat(SimpleAirport airport, SimpleUser user, TestClock clock, int duration) {
		this(airport.id, user.id, clock.toDate(), duration);
	}
	
	SimpleStat add(SimpleUsage u) {
		usage.add( u);
		return this;
	}
	SimpleStat add(String airportId, String userId, Date date, int duration) {
		return add( new SimpleUsage(airportId, userId, date, duration) );
	}
	SimpleStat add(SimpleAirport airport, SimpleUser user, TestClock clock, int duration) {
		return add(airport.id, user.id, clock.toDate(), duration);
	}
	SimpleStat add(SimpleStat s) {
		for (SimpleUsage u : s.usage)
			usage.add(new SimpleUsage(u));
		return this;
	}
	

	
	boolean equals(List<DBUsageDetails> usage) {
		if (usage==null || usage.size()!=this.usage.size())
			return false;
			
		// dumb O(n^2) algorithm
		List<SimpleUsage> my = new ArrayList<SimpleUsage>(this.usage);
		for (DBUsageDetails u : usage) {
			for (SimpleUsage su : my)
				if (su.equals(u)) {
					my.remove(su);
					break;
				}
		}
		return my.size()==0;
	}
	

	@SuppressWarnings("deprecation")
	SimpleStat oneDay(TestClock date) {
		SimpleStat stat = new SimpleStat();
		for (SimpleUsage u : usage)
			if (date.time.getYear() == u.date.getYear() && 
				date.time.getMonth() == u.date.getMonth() &&
				date.time.getDate() == u.date.getDate() )
					stat.usage.add(u);
		return stat;
	}
	
	SimpleStat oneUser(String userId) {
		SimpleStat stat = new SimpleStat();
		for (SimpleUsage u : usage)
			if (u.userId.equalsIgnoreCase( userId) )
					stat.usage.add(u);
		return stat;
	}

	SimpleStat oneAirport(String airportId) {
		SimpleStat stat = new SimpleStat();
		for (SimpleUsage u : usage)
			if (u.airportId.equalsIgnoreCase( airportId) )
					stat.usage.add(u);
		return stat;
	}
	
	public boolean sameUserUsage(List<DBUsageDetails> stat, SimpleUser user, TestClock date) {
		return oneDay(date).oneUser(user.id).equals( stat);
	}

	public boolean sameAirportUsage(List<DBUsageDetails> stat, SimpleAirport airport, TestClock date) {
		return oneDay(date).oneAirport( airport.id).equals( stat);
	}

}
